package yxy.game.pm2.bean.db.newDao;


import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.PlainsArenaRecordPo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liangjianbin
 * @date 2022/3/1 8:40
 **/
public class PlainsArenaRecordDAO {

    private static final String
            REPLACE_ONE = "replace into `plainsArenaRecord` (`pCode`,`code`,`date`,`recordCodes`,`isWin`,`infoA`,`infoB`,`updateTime`) " +
            "values (?,?,?,?,?,?,?,?)",
            SELECT_ALL = "select * from `plainsArenaRecord` where `pCode` = ?",
            DELETE = "delete from `plainsArenaRecord` where `pCode` = ? and `updateTime` != ?";

    public static List<PlainsArenaRecordPo> getList(String playerCode) {
        return DbManager.instance().executeQueryObjectList(SELECT_ALL, new Object[]{playerCode}, PlainsArenaRecordPo.builder);
    }

    public static boolean replace(List<PlainsArenaRecordPo> plainsArenaRecordPoList) {
        List<Object[]> list = new ArrayList<>();
        for (PlainsArenaRecordPo po : plainsArenaRecordPoList) {
            Object[] objects = new Object[]{po.getPlayerCode(), po.getCode(), po.getDate(), po.getRecordCodes(), po.isWin(),
                    po.getInfoA(), po.getInfoB(), po.getUpdateTime()};
            list.add(objects);
        }
        int[] ints = DbManager.instance().executeBatchCommand(REPLACE_ONE, list);
        return ints.length == plainsArenaRecordPoList.size();
    }

    public static void delete(String pCode, long updateTime) {
        int i = DbManager.instance().executeCommand(DELETE, new Object[]{pCode, updateTime});

    }

    public static PlainsArenaRecordPo findOne(String playerCode) {
        String sql = "select * from `plainsArenaRecord` where `pCode` = ? ";
        List<PlainsArenaRecordPo> list = DbManager.instance().executeQueryObjectList(sql, new Object[]{playerCode}, PlainsArenaRecordPo.builder);
        return (list != null && list.size() > 0) ? list.get(0) : null;
    }
}
